package com.fr.plugin.utils;

import com.fr.decision.authority.AuthorityContext;
import com.fr.decision.authority.data.User;
import com.fr.decision.webservice.login.LogInOutResultInfo;
import com.fr.decision.webservice.utils.DecisionServiceConstants;
import com.fr.decision.webservice.v10.login.LoginService;
import com.fr.decision.webservice.v10.login.event.LogInOutEvent;
import com.fr.decision.webservice.v10.user.UserService;
import com.fr.event.EventDispatcher;
import com.fr.log.FineLoggerFactory;
import com.fr.stable.StringUtils;
import com.fr.stable.query.QueryFactory;
import com.fr.stable.query.restriction.RestrictionFactory;
import com.fr.web.utils.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

public class FRUtils {
    /**
     * 判断用户是否存在
     * @param userName
     * @return
     */
    public static boolean isUserExist(String userName){
        if (StringUtils.isEmpty(userName)) {
            return false;
        } else {
            try {
                List var1 = AuthorityContext.getInstance().getUserController().find(QueryFactory.create().addRestriction(RestrictionFactory.eq("userName", userName)));
                return var1 != null && !var1.isEmpty();
            } catch (Exception var2) {
                FineLoggerFactory.getLogger().error(var2.getMessage());
                return false;
            }
        }
    }

    /**
     * 判断是否登录FR
     * @param req
     * @return
     */
    public static boolean isLogin(HttpServletRequest req){
        return LoginService.getInstance().isLogged(req);
    }

    /**
     * 帆软登录
     * @param httpServletRequest
     * @param httpServletResponse
     * @param userName
     * @param url
     */
    public static void login(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String userName,String url){

        FineLoggerFactory.getLogger().info("FRLOG:用户名:"+userName);
        FineLoggerFactory.getLogger().info("FRLOG:跳转链接:"+url);


        //判断用户名是否为空
        if(!Utils.isNullStr(userName)){
            if(isUserExist(userName)){
                String FRToken = "";

                try {
                    HttpSession session = httpServletRequest.getSession(true);

                    FRToken = LoginService.getInstance().login(httpServletRequest, httpServletResponse, userName);

                    httpServletRequest.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,FRToken);

                    session.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, FRToken);
                    EventDispatcher.fire(LogInOutEvent.LOGIN,new LogInOutResultInfo(httpServletRequest,httpServletResponse,userName,true));
                    FineLoggerFactory.getLogger().info("FRLOG:登陆成功!");

                    if(!Utils.isNullStr(url)){
                        httpServletResponse.sendRedirect(url);
                    }
                } catch (Exception e) {
                    ResponseUtils.failedResponse(httpServletResponse,"登录异常，请联系管理员!");
                    FineLoggerFactory.getLogger().info("FRLOG:登录异常，请联系管理员!");
                    FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage());
                }
            }else{
                ResponseUtils.failedResponse(httpServletResponse,"用户在报表系统中不存在!");
                FineLoggerFactory.getLogger().info("FRLOG:用户在报表系统中不存在!");
            }
        }else{
            ResponseUtils.failedResponse(httpServletResponse,"用户名不能为空!");
            FineLoggerFactory.getLogger().info("FRLOG:用户名不能为空!");
        }
    }

    /**
     *
     * @param httpServletRequest
     * @param httpServletResponse
     */
    public static void logout(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse)
    {
        if(!isLogin(httpServletRequest)){
            return ;
        }

        try {
            LoginService.getInstance().logout(httpServletRequest,httpServletResponse);
        } catch (Exception e) {
            ResponseUtils.failedResponse(httpServletResponse,"登出异常，请联系管理员!");
            FineLoggerFactory.getLogger().info("FRLOG:登出异常，请联系管理员!");
            FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage());
        }
    }

    /**
     * 打印FR日志
     * @param message
     */
    public static void FRLogInfo(String message){
        FineLoggerFactory.getLogger().info("FRLOG:"+message);
    }

    /**
     * 根据用户名获取用户信息
     * @param userName
     * @return
     */
    public static User getFRUserByUserName(String userName){
        try {
            return UserService.getInstance().getUserByUserName(userName);
        } catch (Exception e) {
            FRLogInfo("获取用户信息异常:"+e.getMessage());
        }

        return null;
    }

    /**
     * 解密FR密码
     * @param password
     * @return
     */
//    public static String decryptFRPsd(String password){
//        FRLogInfo("解密密码:"+password);
//        return TransmissionTool.decrypt(password);
//    }

    /**
     * 获取带参数的访问链接
     * @return
     */
    public static String getAllUrl(HttpServletRequest httpServletRequest){
        return  WebUtils.getOriginalURL(httpServletRequest);
    }
}
